package com.usian.controller;

import com.usian.pojo.Course;
import com.usian.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping("/course")
@Controller
public class CourseController {

    @Autowired
    private CourseService courseService;

    @RequestMapping("/toAddCourse")
    public String toAddCourse(){
        return "addCourse";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,Model model){
        Course course = courseService.findById(id);
        model.addAttribute("course",course);
        return "update";
    }

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Course> courseList = courseService.findAll();
        model.addAttribute("courseList",courseList);
        return "index";
    }

    @RequestMapping("/insertCourse")
    public void insertCourse(Course course, HttpServletResponse response) throws IOException {
        courseService.insertCourse(course);
        response.sendRedirect("/course/findAll");
    }

    @RequestMapping("/deleteById")
    public void deleteById(Integer id,HttpServletResponse response) throws IOException {
        courseService.deleteById(id);
        response.sendRedirect("/course/findAll");
    }

    @RequestMapping("update")
    public void update(Course course,HttpServletResponse response) throws IOException {
        courseService.update(course);
        response.sendRedirect("/course/findAll");
    }
}
